const { SERVER_ERR } = require("../config/error.js");
const blogService = require("../service/blogs.service.js");
const tagService = require("../service/tag.service.js");

class UserController {
  async create(ctx, next) {
    try {
      const { user = {} } = ctx;
      const { id: userId = 1 } = user;
      const {
        content = "",
        title = "",
        tagId = "",
        imageList = "",
        hotNum = 0,
        commentNum = 0,
        description = "",
      } = ctx.request.body;
      const res = await blogService.create({
        title,
        content,
        userId,
        tagId,
        imageList,
        hotNum,
        commentNum,
        description,
      });
      ctx.body = {
        code: 200,
        message: "创建成功",
        data: res,
      };
    } catch (error) {
      console.log(error);
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async delete(ctx, next) {
    try {
      const ids = ctx.request.body?.ids || [];
      const res = await blogService.delete(ids);
      ctx.body = {
        code: 200,
        message: "删除成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async page(ctx, next) {
    try {
      const { user = {} } = ctx;
      const { id: userId = 1 } = user;
      const { pageNum, pageSize, title, tagId } = ctx.request.body;
      const res = await blogService.page({ pageNum, pageSize, title, userId, tagId });
      const res2 = await blogService.allNum(userId, title, tagId);
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: res,
        total: res2,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async update(ctx, next) {
    try {
      const { id, content, title, tagId, imageList, hotNum, commentNum, description } = ctx.request.body;
      const res = await blogService.update({ id, content, title, tagId, imageList, hotNum, commentNum, description });
      ctx.body = {
        code: 200,
        message: "更新成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async findName(ctx, next) {
    try {
      const { user = {} } = ctx;
      const { id: userId = 1 } = user;
      const { title } = ctx.request.body;
      const res = await blogService.findName({ title, userId });
      ctx.body = {
        code: 200,
        message: "查询成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
  async detail(ctx, next) {
    try {
      const { user = {} } = ctx;
      const { id: userId = 1 } = user;
      const { id } = ctx.request.params;
      const res = await blogService.detail(id);
      const tagArr = await tagService.select(userId);
      if (res.tag_id) {
        res.tags = tagArr
          .filter((item) => res.tag_id.includes(item.id))
          .map((item) => {
            return item.text;
          });
        blogService.updateHot(id);
      }

      ctx.body = {
        code: 200,
        message: "查询成功",
        data: res,
      };
    } catch (error) {
      ctx.error = error;
      ctx.app.emit("error", SERVER_ERR, ctx);
    }
  }
}

module.exports = new UserController();
